import subprocess
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# 创建数据库引擎
db_host = "localhost"
db_port = 3306
db_name = "ecs"
db_user_name = "root"
db_password = "123456"
url = f"mysql+pymysql://{db_user_name}:{db_password}@{db_host}:{db_port}/{db_name}?charset=utf8"

# 配置会话工厂,创建数据库引擎，session类似于会话工厂
engine = create_engine(url)
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)

if __name__ == "__main__":

    def export_db_table_class(run=False): #run的默认值设置为false
        """将数据库表映射为Python类"""
        if not run:
            return
        output_path = "db_table_class.py" #文件的路径

        cmd = ["python", "-m", "sqlacodegen", url]
        result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8") #subprocess用于管理子进程，可以执行外部的命令
        with open(output_path, "w", encoding="utf-8") as f: #打开文件将结果写入
            f.write(result.stdout)

    export_db_table_class(True)